
// 读取芯片内容(读取的时候ce oe全部拉低，改变地址就可以输出)
void read29() {
  uint32_t addr = address;
  uint32_t count = buff;
  //加上y参数的扩展地址
  read_mode();
  P_WE = 1;
  P_OE = 0;
  P_CE = 0;

  for(uint8_t i=0;i<buff;i++){
    Serial.write(getByte(address+i) & 0xff);
  }

  address += buff;
  
  P_WE = 1;
  P_OE = 1;
  P_CE = 1;
}


void write29() {
                  
  uint32_t addr = address;
  delayMicroseconds(1);
  for (uint8_t i = 0; i < buff; i++) {
    while (!Serial.available());
      pageBuffer[i] = Serial.read();
  }

  write_mode();
  P_WE = 1;
  P_OE = 1;
  P_CE = 0;

  delayMicroseconds(1);

  // 多字节page写入
  if(page>1){
    setByte (0x5555, 0xaa);
    setByte (0x2aaa, 0x55);
    setByte (0x5555, 0xa0);
    for (uint8_t i = 0; i < buff; i++) {
      setByte (addr + i, pageBuffer[i]);
      delayMicroseconds(1);
    }
    P_WE = 1;
    P_OE = 1;
    P_CE = 1;
    delay(10);
  }else{
    // 默认单字节
    if(address==0){
      for (uint8_t i = 0; i < buff; i++) {
        setByte (0x5555, 0xaa);
        setByte (0x2aaa, 0x55);
        setByte (0x5555, 0xa0);
        setByte (addr + i, pageBuffer[i]);
        delayMicroseconds(2);//需要加延时
      }  
    }
    for (uint8_t i = 0; i < buff; i++) {
      setByte (0x5555, 0xaa);
      setByte (0x2aaa, 0x55);
      setByte (0x5555, 0xa0);
      setByte (addr + i, pageBuffer[i]);
      delayMicroseconds(10);//需要加延时
    }
    P_WE = 1;
    P_OE = 1;
    P_CE = 1;
    delayMicroseconds(10);
  }
  


  check_buff29();
//  Serial.println("ok");
}




void check_buff29() {
  uint32_t addr = address;
  read_mode();
  //读数据校验
  P_WE = 1;
  P_OE = 0;
  P_CE = 0;
  delayMicroseconds(10);
  int has_error = 0;
  for (int i = 0; i < buff ; i++) {
    uint8_t rd = getByte(addr + i);
    if (pageBuffer[i] != rd) {
      Serial.print("0x");
      Serial.print(addr + i, HEX);
      Serial.print("=");
      Serial.print(rd, HEX);
      Serial.print(",not:");
      Serial.print(pageBuffer[i], HEX);
      Serial.print(" ");
      has_error = 1;
    }
  }
  if (has_error) {
      Serial.print("error");
  }else{
      Serial.println("ok");
  }
  P_WE = 1;
  P_OE = 1;
  P_CE = 1;
}
